package org.jeecg.modules.controller.aiservice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.annotation.CheckHeader;
import org.jeecg.modules.entity.DbCity;
import org.jeecg.modules.entity.DbPurpose;
import org.jeecg.modules.service.IDbCityService;
import org.jeecg.modules.service.IDbPurposeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/aiService")
@Api(tags = "AI调用接口")
@Slf4j
public class AIServiceController {
    @Autowired
    private IDbPurposeService dbPurposeService;
    @Autowired
    private IDbCityService dbCityService;

    @ApiOperation(value = "分页列表查询合作意向", notes = "分页列表查询合作意向")
    @GetMapping(value = "/purpose/list")
    @CheckHeader
    public Result<IPage<DbPurpose>> queryPageList(DbPurpose dbPurpose,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        Map<String, String> headersMap = Collections.list(req.getHeaderNames()).stream()
                .collect(Collectors.toMap(
                        headerName -> headerName,
                        req::getHeader
                ));
        log.info("请求参数dbPurpose : {}, 请求头 : {}",dbPurpose,headersMap);

        QueryWrapper<DbPurpose> queryWrapper = QueryGenerator.initQueryWrapper(dbPurpose, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<DbPurpose> page = new Page<DbPurpose>(pageNo, pageSize);
        IPage<DbPurpose> pageList = dbPurposeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "造价评估", notes = "造价评估")
    @GetMapping(value = "/estimate/get")
    @CheckHeader
    public Result evaluate(Integer point){
//        功能点耗时率
        Double speed = 7.01;
//        规模变更因子
        Double scaleFactor = 1.39;
//        应用领域调整因子
        Double fieldFactor = 1.00;
//        人月折算系数`
        Double manMonthFactor = 176.00;
//        人月费用
        Double manMonthCost = 18331.00;
//        工作量(总功能点数*功能点耗时率*规模变更因子*应用领域调整因子/人月折算系数)
        Double workload = (point*speed*scaleFactor*fieldFactor)/manMonthFactor;
//        预估报价
        Double estimatedQuote = workload*manMonthCost;
        BigDecimal bdEstimatedQuote = new BigDecimal(estimatedQuote).setScale(2, RoundingMode.HALF_UP);
        Map<String ,BigDecimal> map = new HashMap<>();
        map.put("estimatedQuote",bdEstimatedQuote);
        return Result.ok(map);
    }
    /**
     * 省市区三级查询
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value="db_city-省市区三级查询", notes="db_city-省市区三级查询")
    @CheckHeader
    public Result<List<DbCity>> city(){
        List<DbCity> list = dbCityService.getRegions();
        return Result.OK(list);
    }
}
